package com.hyjr.common.utils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class PropertyUtil {

	private static Map<String, Map<String, String>> propertyMap = new HashMap<String, Map<String, String>>();
	
	private static void loadPropeties(String fileName) {
		Properties prop = new Properties();
		Map<String, String> property = new HashMap<String, String>();
		try {
			// 读取属性文件.properties
			InputStream in = PropertyUtil.class.getClassLoader().getResourceAsStream(fileName + ".properties");
			prop.load(in); /// 加载属性列表
			Iterator<String> it = prop.stringPropertyNames().iterator();
			while (it.hasNext()) {
				String key = it.next();
				property.put(key, prop.getProperty(key));
			}
			in.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		propertyMap.put(fileName, property);
	}
	
	public static String getPropety(String fileName, String attrName) {
		if(propertyMap.get(fileName) == null) {
			loadPropeties(fileName);
		}
		
		Map<String, String> property = propertyMap.get(fileName);
		
		if(property != null) {
			return property.get(attrName);
		}
		
		return null;
	}
}